var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "mobile/site", "mobile/services/shoppingService", "mobile/services/shoppingCartService", "react", "maishu-ui-toolkit", "controls/spiner-component"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const site_1 = require("mobile/site");
    const shoppingService_1 = require("mobile/services/shoppingService");
    const shoppingCartService_1 = require("mobile/services/shoppingCartService");
    const React = require("react");
    const ui = require("maishu-ui-toolkit");
    const spiner_component_1 = require("controls/spiner-component");
    class OrderPage extends spiner_component_1.SpinerComponent {
        constructor(props) {
            super(props);
            this.shop = this.props.createService(shoppingService_1.ShoppingService);
            this.shoppingCart = this.props.createService(shoppingCartService_1.ShoppingCartService);
            this.state = { order: null };
            this.setAddress = (address, order) => {
                Object.assign(this.state.order, order);
                this.state.order.ReceiptAddress = address;
                this.setState(this.state);
            };
        }
        loadData() {
            return __awaiter(this, void 0, void 0, function* () {
                let id = this.props.data.id;
                let order = yield this.shop.order(id);
                return { order };
            });
        }
        showCoupons() {
            this.props.app.redirect("shopping/orderCoupons", { orderId: this.state.order.Id });
        }
        confirmOrder() {
            let order = this.state.order;
            let orderId = order.Id;
            let remark = order.Remark;
            let invoice = order.Invoice;
            return this.shop.confirmOrder(orderId, remark, invoice).then(() => {
                let itemIds = order.OrderDetails.map(o => o.ProductId);
                this.shoppingCart.removeItemsByProductIds(itemIds);
                this.props.app.redirect("shopping/purchase", { id: orderId }); // `shopping_purchase?id=${order.Id}`);
            });
        }
        showReceiptList() {
            let routeValue = { callback: this.setAddress, orderId: this.state.order.Id };
            this.props.app.redirect("user/receiptList", routeValue); //'user_receiptList'
        }
        render() {
            let order = this.state.order;
            return React.createElement(React.Fragment, null,
                React.createElement("header", { key: "h" }, site_1.defaultNavBar(this.props.source, { title: '确认订单' })),
                React.createElement("footer", { key: "f" },
                    React.createElement("div", { className: "container", style: { paddingTop: 10, paddingBottom: 10 } },
                        React.createElement("button", { onClick: () => this.confirmOrder(), className: "btn btn-block btn-primary", disabled: (order.ReceiptAddress || "") == "" }, "\u63D0\u4EA4\u8BA2\u5355"))),
                React.createElement("section", { key: "v" },
                    React.createElement("div", { className: "container" },
                        React.createElement("h4", { className: "text-primary" }, "\u6536\u8D27\u4FE1\u606F"),
                        React.createElement("a", { style: { minHeight: 40, display: order.ReceiptAddress ? 'none' : 'block' }, onClick: () => this.showReceiptList() },
                            React.createElement("div", { className: "alert alert-danger text-center" }, "\u70B9\u51FB\u8FD9\u91CC\u8BBE\u7F6E\u6536\u8D27\u4FE1\u606F")),
                        React.createElement("a", { onClick: () => this.showReceiptList(), className: "address", style: { minHeight: 40, display: order.ReceiptAddress ? 'block' : 'none' } },
                            React.createElement("div", { className: "pull-left", style: { paddingRight: 20 } },
                                React.createElement("span", { className: "small" }, order.ReceiptAddress)),
                            React.createElement("div", { className: "pull-right" },
                                React.createElement("i", { className: "icon-chevron-right" })))),
                    React.createElement("hr", { style: { margin: 0, borderTopWidth: 10 } }),
                    React.createElement("div", { className: "container" },
                        React.createElement("h4", { className: "text-primary" }, "\u8D2D\u7269\u6E05\u5355")),
                    React.createElement("div", { className: "container" },
                        React.createElement("ul", { "data-bind": "foreach: order.OrderDetails", className: "list-group row", style: { marginBottom: 0 } }, order.OrderDetails.map((o, i) => (React.createElement("li", { key: i, "data-bind": "visible:ko.unwrap(Price) >= 0", className: "list-group-item" },
                            React.createElement("div", { className: "pull-left", style: { width: 60, height: 60 } },
                                React.createElement("img", { src: o.ImageUrl, className: "img-responsive", ref: (e) => e ? ui.renderImage(e) : null })),
                            React.createElement("div", { style: { marginLeft: 70 } },
                                React.createElement("div", { style: { marginBottom: 10 } },
                                    React.createElement("a", { href: `#home_product?id=${o.ProductId}`, className: "title" }, o.ProductName)),
                                React.createElement("div", { className: "pull-left" },
                                    React.createElement("span", { className: "price" },
                                        "\uFFE5",
                                        o.Price.toFixed(2)),
                                    (o.Score ? React.createElement("span", null,
                                        " + ",
                                        o.Score,
                                        " \u79EF\u5206") : null)),
                                React.createElement("div", { className: "pull-right" },
                                    React.createElement("span", { style: { paddingLeft: 10 } },
                                        "X ",
                                        o.Quantity))),
                            React.createElement("div", { className: "clearfix" })))))),
                    React.createElement("hr", { style: { margin: 0, borderTopWidth: 10 } }),
                    React.createElement("div", { className: "container" },
                        React.createElement("h4", { className: "pull-left" }, "\u914D\u9001\u65B9\u5F0F"),
                        React.createElement("div", { className: "pull-right", style: { paddingTop: 6 } },
                            React.createElement("span", null,
                                "\u5FEB\u9012 \u8FD0\u8D39\uFF1A",
                                React.createElement("span", { className: "price" },
                                    "\uFFE5",
                                    order.Freight.toFixed(2))))),
                    React.createElement("hr", { style: { margin: 0, borderTopWidth: 10 } }),
                    order.CouponTitle ?
                        React.createElement("div", { onClick: () => this.showCoupons(), className: "container" },
                            React.createElement("h4", { className: "pull-left" }, "\u4F18\u60E0\u5238"),
                            React.createElement("div", { className: "pull-right", style: { paddingTop: 6 } },
                                React.createElement("span", { style: { paddingRight: 4 } }, order.CouponTitle),
                                React.createElement("i", { className: "icon-chevron-right" }))) : null,
                    order.CouponTitle ?
                        React.createElement("hr", { "data-bind": "visible:order.CouponTitle", style: { margin: 0, borderTopWidth: 10 } })
                        : null,
                    React.createElement("div", { className: "container", onClick: () => this.props.app.redirect("shopping/invoice", {
                            callback: (invoice) => {
                                this.state.order.Invoice = invoice;
                                this.setState(this.state);
                            }
                        }) },
                        React.createElement("h4", { className: "pull-left" }, "\u53D1\u7968\u4FE1\u606F"),
                        React.createElement("div", { className: "pull-right", style: { paddingTop: 6 } },
                            React.createElement("span", { style: { paddingRight: 10 } }, order.Invoice),
                            React.createElement("i", { className: "icon-chevron-right" }))),
                    React.createElement("hr", { style: { margin: 0, borderTopWidth: 10 } }),
                    React.createElement("div", { className: "container", style: { padding: 10 } },
                        React.createElement("input", { name: "remark", type: "text", multiple: true, style: { width: '100%', height: 40, border: '1px solid #dddddd' }, placeholder: " \u82E5\u4F60\u5BF9\u8BA2\u5355\u6709\u7279\u6B8A\u6027\u8981\u6C42\uFF0C\u53EF\u4EE5\u5728\u6B64\u5907\u6CE8" })),
                    React.createElement("div", { className: "container", style: { marginBottom: 100 } },
                        React.createElement("div", { className: "row" },
                            React.createElement("div", { className: "col-xs-4" }, "\u5546\u54C1"),
                            React.createElement("div", { className: "col-xs-8 text-right" },
                                React.createElement("span", { style: { paddingRight: 4 } }, "+"),
                                React.createElement("span", { className: "price" },
                                    "\uFFE5",
                                    order.Amount.toFixed(2)))),
                        React.createElement("div", { className: "row" },
                            React.createElement("div", { className: "col-xs-4" }, "\u8FD0\u8D39"),
                            React.createElement("div", { className: "col-xs-8 text-right" },
                                React.createElement("span", { style: { paddingRight: 4 } }, "+"),
                                React.createElement("span", { className: "price" },
                                    "\uFFE5",
                                    order.Freight.toFixed(2)))),
                        React.createElement("div", { className: "row" },
                            React.createElement("div", { className: "col-xs-4" }, "\u4F18\u60E0"),
                            React.createElement("div", { className: "col-xs-8 text-right" },
                                React.createElement("span", { style: { paddingRight: 6 } }, "-"),
                                React.createElement("span", { className: "price" },
                                    "\uFFE5",
                                    (order.Discount || 0).toFixed(2)))),
                        React.createElement("div", { className: "col-xs-12", style: { padding: 0, paddingTop: 8 } },
                            React.createElement("div", { className: "pull-right" },
                                React.createElement("span", null,
                                    "\u603B\u8BA1\uFF1A",
                                    React.createElement("strong", { className: "price" },
                                        "\uFFE5",
                                        order.Sum.toFixed(2))))))));
        }
    }
    exports.default = OrderPage;
});
